﻿

#region Using Statements

// System References
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;

// XNA References
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

// Internal References
using Freeform.Framework;
using Freeform.Framework.Graphics;
using Game = Freeform.Framework.Game;

#endregion


namespace Freeform.Framework.Menu
{
    /// <summary>
    /// Represents the base class for a simple text based menu item.
    /// </summary>
    public class MenuItem : Entity
    {

        #region Events

        /// <summary>
        /// This event is fired when the mouse leaves the menu item.
        /// </summary>
        public event EventHandler ItemLeft;


        /// <summary>
        /// This event is fired when the mouse hovers over the menu item.
        /// </summary>
        public event EventHandler ItemHighlighted;


        /// <summary>
        /// This event is fired when the user left clicks on the menu item.
        /// </summary>
        public event EventHandler ItemClicked;


        /// <summary>
        /// This event is fired when the user right clicks on the menu item.
        /// </summary>
        public event EventHandler ItemRightClicked;

        #endregion


        #region Members

        /// <summary>
        /// Gets whether or not the item has been highlighted by the mouse.
        /// </summary>
        public Boolean IsHighlighted { get; private set; }

        #endregion


        #region Initialisation

        public MenuItem(World World)
            : base(World)
        {
            IsHighlighted = false;
        }

        #endregion


        #region Update

        /// <summary>
        /// Updates the menu item.
        /// </summary>
        /// <param name="Time">Provides a snapshot of timing values.</param>
        protected override void BeginUpdate(GameTime Time)
        {
            //base.Update(Time);

            if (IsMouseOver())
            {
                // Check if newly highlighted.
                if (!IsHighlighted && ItemHighlighted != null)
                    ItemHighlighted(this, new EventArgs());
                IsHighlighted = true;

                // Check if item was left clicked.
                if (Game.Input.MouseState_Current.LeftButton == ButtonState.Pressed &&
                    Game.Input.MouseState_Previous.LeftButton == ButtonState.Released &&
                    ItemClicked != null)
                    ItemClicked(this, new EventArgs());

                // Check if item was right clicked.
                if (Game.Input.MouseState_Current.RightButton == ButtonState.Pressed &&
                    Game.Input.MouseState_Previous.RightButton == ButtonState.Released &&
                    ItemRightClicked != null)
                    ItemRightClicked(this, new EventArgs());
            }
            else
            {
                // Check if item was recently unhighlighted.
                if (IsHighlighted && ItemLeft != null)
                    ItemLeft(this, new EventArgs());
                IsHighlighted = false;
            }
        }

        #endregion

    }
}